package com.study.ui.test

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lbj.core.ext.pageAnim
import com.lbj.core.ext.startPage
import com.study.databinding.AdapterTestBinding

/**
 * 作者：李炳军
 * 创建日期：2021-03-31  11:03
 * 释义：
 */
class TestAdapter(val act: Activity) :
    ListAdapter<TestBean, TestAdapter.ViewHolder>(TestBeanCallback()) {

    inner class ViewHolder(private val binding: AdapterTestBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(position: Int) {
            binding.position = position
            binding.result = getItem(position)
            binding.presenter = Presenter()
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            AdapterTestBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class Presenter {
        fun onItemClick(v: View, result: TestBean) {
            if (result.toClass != null) {
                act.startPage(result.toClass)
            }
        }
    }
}

class TestBeanCallback : DiffUtil.ItemCallback<TestBean>() {
    override fun areItemsTheSame(oldItem: TestBean, newItem: TestBean): Boolean {
        return true
    }

    override fun areContentsTheSame(oldItem: TestBean, newItem: TestBean): Boolean {
        return true
    }
}

data class TestBean(val id: Int, val title: String, val color: Int, val toClass: Class<*>? = null)